﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Model;

public partial class ViewbyProduct_ZTEElectronicsList_PackageProductSNExport : System.Web.UI.Page
{
    public string packageProductSN;
    private Test test;
    private TestItem ti;
    private TestInfo tinfo;
    private PackageProduct pp;

    public ViewbyProduct_ZTEElectronicsList_PackageProductSNExport()
    {
        test = new Test();
        ti = new TestItem();
        pp = new PackageProduct();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Button6.Attributes.Add("onclick", "PasteData()");
        }
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        //此处粘贴录入的都为成品序列号
        string str = this.myVal.Value;
        string[] strs = str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string st in strs)
        {
            if (!ProductSNCompare(st))
                this.ListBox1.Items.Add(st.Trim());
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        if (this.ListBox1.Items.Count == 0 || this.ListBox1.Items.Count > 50)
        {
            string str = "请保证产品序列号数量大于0且小于50！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            return;
        }
        StringBuilder sbFail = new StringBuilder();
        List<string> strPass = new List<string>();
        foreach (ListItem item in this.ListBox1.Items)
        {
            packageProductSN = item.Text;
            string ReportFile = Server.MapPath("~/ViewTest/ViewYourSelf/ViewTestItem.rpt");
            string ExcelFileName = Server.MapPath("~/ZedGraphImages/" + packageProductSN + ".pdf");
            if (!ExportToExcel1(ReportFile, ExcelFileName))
            {
                sbFail.Append(packageProductSN);
                sbFail.Append(',');
            }
            else
            {
                strPass.Add(packageProductSN);
            }
        }
        if (sbFail.Length > 0)
        {
            string str = sbFail.ToString();
            str = str.TrimEnd(',');
            str = "以下成品序列号没有测试数据，或不是有效的成品序列号：" + str;
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            return;
        }
        if (strPass.Count > 0)
        {
            string directoryName = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
            string directoryPath = Server.MapPath("~/ZedGraphImages/" + directoryName);
            string targetZipFile = Server.MapPath("~/ZedGraphImages/" + directoryName + ".zip");
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            foreach (string str in strPass)
            {
                File.Move(Server.MapPath("~/ZedGraphImages/" + str + ".pdf"), directoryPath + "\\" + str + ".pdf");
            }

            SharpZipLibHelper ziphelper = new SharpZipLibHelper();
            ziphelper.ZipFileOrFolder(directoryPath, true, targetZipFile);
            Directory.Delete(directoryPath, true);

            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=ZTESParamTestData.zip");
            string filename = targetZipFile;
            Response.TransmitFile(filename);
        }
    }

    public bool ExportToExcel1(string ReportFile, string ExcelFileName)
    {
        try
        {
            ReportDocument ReportDOC = new ReportDocument();
            DiskFileDestinationOptions FileOPS = new DiskFileDestinationOptions();
            ExportOptions ExOPS;
            ReportDOC.Load(ReportFile);

            //string productSN = CommHelper.GetProductSN(packageProductSN);
            string productSN = pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductSN;
            if (string.IsNullOrEmpty(productSN))
            {
                throw new Exception("此成品序列号没有关联过腔体序列号!");
            }
            else
            {
                tinfo = test.GetTestbyLastSN(productSN);
                if (string.IsNullOrEmpty(tinfo.ProductSN))
                {
                    throw new Exception("没有相应测试数据!");
                }
                else
                {
                    TextObject txtProductSN = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text4"];
                    txtProductSN.Text = "成品序列号：" + tinfo.ProductSN;
                    TextObject txtTestTime = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text13"];
                    txtTestTime.Text = "测试时间：" + tinfo.TestTime;
                    TextObject txtProductTypeID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text14"];
                    txtProductTypeID.Text = "产品类型：" + tinfo.ProductTypeID;
                    TextObject txtUserID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text15"];
                    txtUserID.Text = "测试员工号：" + tinfo.UserID;
                    TextObject txtENAID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text16"];
                    txtENAID.Text = "矢网编号：" + tinfo.ENAID;
                    TextObject txtSpecVersion = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text2"];
                    txtSpecVersion.Text = "指标单版本：" + tinfo.SpecVersion;
                    TextObject txtIsPass = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text17"];
                    txtIsPass.Text = "是否通过：" + tinfo.IsPass;

                    IList<TestItemInfo> dti = ti.GetTestItems(productSN, tinfo.TestTime);
                    ReportDOC.SetDataSource(dti);

                    FileOPS.DiskFileName = ExcelFileName;
                    ExOPS = ReportDOC.ExportOptions;
                    ExOPS.DestinationOptions = FileOPS;
                    ExOPS.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
                    ExOPS.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                    ReportDOC.Export();
                    return true;
                }
            }
        }
        catch
        {
            return false;
        }
    }
}
